//
//  XKUpdatePasswordController.m
//  XKJF
//
//  Created by xyg on 2016/12/17.
//  Copyright © 2016年 xiaokang100. All rights reserved.
//

#import "XKUpdateLoginPwdController.h"
#import "XKUpdateLoginPwdView.h"

#import "XKMainNavigationController.h"
#import "XKLoginViewController.h"

@interface XKUpdateLoginPwdController ()<UITextFieldDelegate>

@property (nonatomic, strong) XKUpdateLoginPwdView *changePwdView;// 修改密码页面

@end

@implementation XKUpdateLoginPwdController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)setupUI {
    self.navigationItem.title = @"修改登录密码";
    
    self.changePwdView = [XKUpdateLoginPwdView new];
    self.changePwdView.frame = self.view.bounds;
    [self.view addSubview:self.changePwdView];
    
    [self.changePwdView.confirmBtn addTarget:self action:@selector(updatePwdBtnClick:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - 修改登录密码
- (void)updatePwdBtnClick:(UIButton *)sender {
    [self.view endEditing:YES];

    if (![self.changePwdView.pwdTextFieldOne.text isEqualToString:self.changePwdView.pwdTextFieldTwo.text]) {
        [SVProgressHUD showErrorWithStatus:@"新密码两次输入不一致!"];
        return;
    }
    
    [SVProgressHUD show];
    
    NSString *oldPwd   = self.changePwdView.pwdTextFieldZero.text;
    NSString *newPwd   = self.changePwdView.pwdTextFieldTwo.text;
    
    [APIManager POSTRequestWith:APIPOSTRequestState_UpdateLoginPassword parameters:@{@"pwd":newPwd, @"oldpwd":oldPwd} successBlock:^(NSDictionary *successDict) {
        [SVProgressHUD showSuccessWithStatus:successDict[@"msg"]];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            UIWindow *window = [UIApplication sharedApplication].keyWindow;
            XKMainNavigationController *nav = [[XKMainNavigationController alloc] initWithRootViewController:[XKLoginViewController new]];
            window.rootViewController = nav;
        });
        
        [MobClick event:@"UpdateLoginPwd"]; // 友盟事件统计
    } failureBlock:^(NSDictionary *failureDict) {
        
    } errorBlock:^(NSError *error) {
        
    }];

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
